package com.sgai.zero_robot.pop;

import android.app.Activity;
import android.os.Build;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowInsets;
import android.view.WindowInsetsController;
import android.widget.PopupWindow;

import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.RequiresApi;

public abstract class BasePopupWindow {
    protected Activity mContext;

    protected PopupWindow mPopupWindow;

    protected View mPopContentView;

    /**
     * @param context 上下文
     */
    public BasePopupWindow(Activity context) {
        this(context, 0, 0);
    }
    /**
     * @param context 上下文
     * @param width   可见区域的宽度 单位dp
     * @param height  可见区域的高度
     */
    public BasePopupWindow(Activity context, int width, int height) {
        mContext = context;
        mPopContentView = LayoutInflater.from(mContext).inflate(getContentView(), null);
        mPopContentView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
        );
        mPopupWindow = new PopupWindow(mPopContentView, width==0?ViewGroup.LayoutParams.MATCH_PARENT:width, height==0?ViewGroup.LayoutParams.MATCH_PARENT:height);
        mPopupWindow.setClippingEnabled(false);
        init();
    }
    public <T extends View> T findviewById(@IdRes int id){
        return mPopContentView.findViewById(id);
    }
    // 拦截返回键
    protected void setFocusableInTouchMode(boolean focusable){
        mPopContentView.setFocusableInTouchMode(focusable);
    }
    // 点击空白区域
    protected void setOutsideTouchable(boolean outsideTouchable){
        mPopupWindow.setOutsideTouchable(outsideTouchable);
    }
    //设置动画
    protected void setAnimationStyle(int style){
        mPopupWindow.setAnimationStyle(style);
    }
    //设置焦点
    protected void setFocusable(boolean focusable){
        mPopContentView.setFocusable(focusable);
    }
    //展示
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP_MR1)
    public void show(Activity activity,View view) {
        if (mPopupWindow.isShowing()) {
            return;
        }
        if (view == null){
            return;
        }
        if (activity.isDestroyed()||activity.isFinishing()){
            return;
        }

        mPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
        hideSystemUI();
    }
    private void hideSystemUI() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            WindowInsetsController controller = mContext.getWindow().getInsetsController();
            if (controller != null) {
                controller.hide(WindowInsets.Type.navigationBars() | WindowInsets.Type.statusBars());
                controller.setSystemBarsBehavior(
                        WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                );
            }
        } else {
            mContext.getWindow().getDecorView().setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            );
        }
    }
    public void showAtLocation(Activity activity,View view ,int gravity, int x, int y) {
        if (mPopupWindow.isShowing()) {
            return;
        }
        if (view == null){
            return;
        }
        if (activity.isDestroyed()||activity.isFinishing()){
            return;
        }

        mPopupWindow.showAtLocation(view, gravity, x, y);
        hideSystemUI();
    }
    public void showButton(Activity activity,View view) {
        if (mPopupWindow.isShowing()) {
            return;
        }
        if (view == null){
            return;
        }
        if (activity.isDestroyed()||activity.isFinishing()){
            return;
        }

        mPopupWindow.showAsDropDown(view);
        hideSystemUI();
    }
    /**
     * 隐藏弹出框
     */
    public void dismiss() {
        if (mPopupWindow != null) {
            if (mPopupWindow.isShowing()) {
                mPopupWindow.dismiss();
            }
        }
    }
    // 判断当前的线程是否是UI线程
    protected boolean checkOnMainThread() {
        return Looper.myLooper() != Looper.getMainLooper();
    }
    public abstract void init();
    public abstract @LayoutRes
    int getContentView();

    public boolean isShowing(){
        if (mPopupWindow!=null){
            return mPopupWindow.isShowing();
        }
        return false;
    }

}
